package com.jeeplus.modules.wms.util;

import org.apache.poi.hssf.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * Created by lichaohui on 2018/4/26.
 */
public  class ExportU {
    public  void  export(HttpServletResponse response, String str, List<Map<String,Object>> list){
        //excel标题
        String[] title = {"物料名称","物料编码","作业类型","拣货数量","拣货重量","收货数量","收货重量"};


        //excel文件名
        String fileName = "出入库统计表"+str+".xls";

        //sheet名
        String sheetName = "出入库统计";
//创建HSSFWorkbook
        HSSFWorkbook wb = getHSSFWorkbook(sheetName, title, list, null);

//响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"iso-8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.reset();
            response.setContentType("application/x-download ");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    //创建工作簿
    public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,List<Map<String,Object>> list, HSSFWorkbook wb){

        // 第一步，创建一个HSSFWorkbook，对应一个Excel文件
        if(wb == null){
            wb = new HSSFWorkbook();
        }

        // 第二步，在workbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);

        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        HSSFRow row = sheet.createRow(0);

        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        //声明列对象
        HSSFCell cell = null;

        //创建标题
        for(int i=0;i<title.length;i++){
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }

        //创建内容
        for(int i=0;i<list.size();i++){
            row = sheet.createRow(i + 1);
            //将内容按顺序赋给对应的列对象
            row.createCell(0).setCellValue(list.get(i).get("name")+"");
            row.createCell(1).setCellValue(list.get(i).get("code")+"");
            row.createCell(2).setCellValue(list.get(i).get("type")+"");
            row.createCell(3).setCellValue(list.get(i).get("amount")+"");
            row.createCell(4).setCellValue(list.get(i).get("pweight")+"");
            row.createCell(5).setCellValue(list.get(i).get("recive")+"");
            row.createCell(6).setCellValue(list.get(i).get("rweight")+"");

        }
        return wb;
    }
    public  void  export1(HttpServletResponse response, String str, List<Map<String,Object>> list){
        //excel标题
        String[] title = {"职位","姓名","作业类型","出库量","入库量"};


        //excel文件名
        String fileName = "作业量统计表"+str+".xls";

        //sheet名
        String sheetName = "作业量统计";
//创建HSSFWorkbook
        HSSFWorkbook wb = getHSSFWorkbook1(sheetName, title, list, null);

//响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //创建工作簿
    public static HSSFWorkbook getHSSFWorkbook1(String sheetName,String []title,List<Map<String,Object>> list, HSSFWorkbook wb){

        // 第一步，创建一个HSSFWorkbook，对应一个Excel文件
        if(wb == null){
            wb = new HSSFWorkbook();
        }

        // 第二步，在workbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);

        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        HSSFRow row = sheet.createRow(0);

        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        //声明列对象
        HSSFCell cell = null;

        //创建标题
        for(int i=0;i<title.length;i++){
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }

        //创建内容
        for(int i=0;i<list.size();i++){
            row = sheet.createRow(i + 1);
            //将内容按顺序赋给对应的列对象
            row.createCell(0).setCellValue(list.get(i).get("position")+"");
            row.createCell(1).setCellValue(list.get(i).get("name")+"");
            row.createCell(2).setCellValue(list.get(i).get("type")+"");
            row.createCell(3).setCellValue(list.get(i).get("outAmount")+"");
            row.createCell(4).setCellValue(list.get(i).get("amount")+"");


        }
        return wb;
    }
}
